package it.globalgamejam.scar.game.world 
{
	import it.globalgamejam.scar.Assets;
	import it.globalgamejam.scar.game.GameWorld;
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.Sfx;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	
	/**
	 * ...
	 * @author N1xx1
	 */
	public class Player extends Entity
	{
		
		private var running:Boolean = false;
		
		public var speedX:Number = 0;
		public var speedY:Number = 0;
		
		public var deltaTime:Number = 0;
		
		public var deltaSpeed:Number = 10;
		
		private var spritemap:Spritemap = new Spritemap(Assets.SCARABEO, 80, 96);
		
		private var sfxWalk1:Sfx = new Sfx(Assets.SOUND_WALK_1);
		private var sfxWalk2:Sfx = new Sfx(Assets.SOUND_WALK_2);
		private var sfxWalk3:Sfx = new Sfx(Assets.SOUND_WALK_3);
		
		public function Player(x:Number, y:Number) 
		{
			super(x + 40, y + 40);
			
			spritemap.add("move", [0, 1, 2, 3], 10, true);
			spritemap.add("idle", [2], 10, true);
			
			spritemap.scaledWidth = 40;
			spritemap.scaledHeight = 40;
			spritemap.originX = 40;
			spritemap.originY = 48;
			spritemap.x = 20;
			spritemap.y = 20;
			graphic = spritemap;
			
			spritemap.play("idle");
			
			layer = 20;
			
			setHitbox(40, 40);
			name = "player";
			type = "player";
		}
		
		override public function update():void 
		{
			super.update();
			
			if (GameWorld(FP.world).isPlaying) {
				var deltaX:Number = 0;
				var deltaY:Number = 0;
				
				if (Input.check(Key.LEFT)) {
					deltaX += -deltaSpeed;
				}
				if (Input.check(Key.RIGHT)) {
					deltaX += deltaSpeed;
				}
				if (Input.check(Key.UP)) {
					deltaY += -deltaSpeed;
				}
				if (Input.check(Key.DOWN)) {
					deltaY += deltaSpeed;
				}
				
				if (deltaX < 0) {
					if (deltaY > 0) {
						spritemap.angle = 135;
					} else if (deltaY < 0) {
						spritemap.angle = 45;
					} else {
						spritemap.angle = 90;
					}
				} else if (deltaX > 0) {
					if (deltaY > 0) {
						spritemap.angle = 225;
					} else if (deltaY < 0) {
						spritemap.angle = 315;
					} else {
						spritemap.angle = 270;
					}
				} else {
					if (deltaY < 0) {
						spritemap.angle = 0;
					} else if (deltaY > 0) {
						spritemap.angle = 180;
					}
				}
				
				if (deltaX == 0) {
					speedX *= 0.9;
					if (speedX > -10 && speedX < 10) speedX = 0;
				} else {
					speedX += deltaX;
					if (speedX < -300) speedX = -300;
					if (speedX > 300) speedX = 300;
				}
				
				if (deltaY == 0) {
					speedY *= 0.9;
					if (speedY > -10 && speedY < 10) speedY = 0;
				} else {
					speedY += deltaY;
					if (speedY < -300) speedY = -300;
					if (speedY > 300) speedY = 300;
				}
				
				deltaTime += FP.elapsed;
				if (deltaTime > 0.2) {
					deltaTime -= 0.2;
					if (running) {
						var n:Number = Math.random() * 3;
						if (n < 1) sfxWalk1.play();
						else if (n < 2) sfxWalk2.play();
						else sfxWalk3.play();
					}
				}
				
				if (deltaX == 0 && deltaY == 0) {
					spritemap.play("idle");
					running = false;
				} else if(!running) {
					spritemap.play("move");
					running = true;
				}
				
				var endposx:Number = 0;
				var endposy:Number = 0;
				
				var types:Array = ["goal", "wall"];
				for each (var type:String in types) {
					var ox:Number = x;
					var oy:Number = y;
					
					var dx:Number = speedX * FP.elapsed;
					var dy:Number = speedY * FP.elapsed;
					
					moveBy(dx, dy, types);
					var edx:Number = x - dx;
					var edy:Number = y - dy;
					
					endposx = x;
					endposy = y;
					if (edx != ox && edy != oy) break;
					else {
						x = endposx - dx;
						y = endposy - dy;
					}
				}
				
				x = endposx;
				y = endposy;
			}
		}
		
		override public function moveCollideX(e:Entity):Boolean
		{
			speedX = 0;
			return true;
		}
		
		override public function moveCollideY(e:Entity):Boolean
		{
			speedY = 0;
			return true;
		}
	}

}